=========
Packaging
=========

.. highlight:: none

How to package SieveManager for Debian_.

.. CAUTION::
    The following commands require that the
    repository is your working directory.


Store the SieveManger version in `vers` and package name in `deb`::

    vers="$(python3 -c 'import sievemgr; print(sievemgr.__version__)')"
    deb="sievemgr_${vers:?}-0_all"


Store your name and email in `maintainer`::

    maintainer='"J. Doe" <jdoe@example.com>'


Create the staging directory tree::

    mkdir -p "${deb:?}/DEBIAN" "${deb:?}/usr/share/doc/sievemgr"


Create the package metadata::

    cat <<EOF >"${deb:?}/DEBIAN/control"
    Package: sievemgr
    Version: ${vers:?}
    Standards-Version: 4.6.2.1
    Architecture: all
    Priority: optional
    Section: utils
    Depends: python3 (>=3.9), python3-cryptography (>=2.4)
    Recommends: python3-dnspython (>=2)
    Maintainer: ${maintainer:?}
    Homepage: https://odkr.codeberg.page/sievemgr
    Vcs-Browser: https://codeberg.org/odkr/sievemgr
    Description: Fully-featured ManageSieve command-line client and library
     SieveManager is a command-line client for uploading, downloading,
     and managing Sieve scripts using the ManageSieve protocol. It can
     also be used as a Python module.
    EOF


Create the copyright file::

    cat <<'EOF' >"${deb:?}/usr/share/doc/sievemgr/copyright"
    Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
    Source: https://odkr.codeberg.page/sievemgr
    Upstream-Name: SieveManager
    Upstream-Contact: Odin Kroeger
    Copyright: 2023 and 2024  Odin Kroeger
    License: GPL-3+

    Files: *
    Copyright: 2023 and 2024  Odin Kroeger
    License: GPL-3+
    EOF


Copy the change log::

    gzip -9n <NEWS.rst >"${deb:?}/usr/share/doc/sievemgr/changelog.gz"


Install SieveManager to the staging directory::

    make DESTDIR="${deb:?}" \
         PIPARGS='--root=$(DESTDIR) --prefix=$(prefix) --no-deps' \
         prefix=/usr \
         install

.. TIP::
    The :file:`makefile` conforms to the *GNU Coding standards*
    (chap. `7.2 <gnu-makefile-conv_>`_).

Debian's Python uses :file:`/usr/lib/python{X}/dist-packages`,
*not* :file:`/usr/lib/python{X}.{Y}/site-packages`::

    pythondir="$(ls -d "${deb:?}"/usr/lib/python*)"
    debiandir="${pythondir%.*}"
    mv "$pythondir" "$debiandir"
    mv "$debiandir/site-packages" "$debiandir/dist-packages"


Compress the manual pages::

    gzip -9n "${deb:?}/usr/share/man/man1/"*.1
    gzip -9n "${deb:?}/usr/share/man/man5/"*.5


Remove Python caches created by :command:`pip`::

    find "${deb:?}" -type d -name __pycache__ -exec rm -rf '{}' +


Create the Debian package::

    dpkg-deb --root-owner-group --build "${deb:?}"


Sign the package::

    debsigs --sign=maint "${deb:?}.deb"

